<template>
  <a-modal width="600px" v-model:visible="visible" title="站点备注编辑" @before-ok="onOk">
    <a-form ref="formRef" :model="form" size="medium" auto-label-width>
      <a-form-item label="备注" field="remark">
        <a-textarea placeholder="请输入备注" v-model="form.remark" allow-clear :auto-size="{minRows:6}"></a-textarea>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import { setSiteRemark } from '@/apis'
import { Message } from '@arco-design/web-vue'
import { reAssign } from '@/utils/common'

const emit = defineEmits(['getTableData'])

const visible = ref(false)
const formRef = ref()

const form = reactive({
  id: undefined,
  remark: undefined
})

// 打开编辑框并回显表单值
const addOrEdit = (item) => {
  reAssign(form, item)
  item && formRef.value?.validate()
  visible.value = true
}

// 点击确认添加/编辑
const onOk = async () => {
  const validateResult = await formRef.value?.validate()
  if (validateResult) return false
  await setSiteRemark(form)
  Message.success('操作成功')
  emit('getTableData')
  return true
}

provide('formRef', formRef)
defineExpose({ addOrEdit })
</script>
